<?php
function baidupc($tasks){

    Global $whitelist; //白名单
    Global $posturl;
    Global $filepath;   //开关文件
    $nul = 0;   //快照为空

    $header = getheader();

    //每页搜索记录
    for($pn = 0;$pn < $tasks['pn'];$pn++){

        $url="https://www.baidu.com/s?wd=".str_replace(' ','%20',$tasks['search'])."&nojs=1&pn=".($pn*10);

        print_f($url."\t");

        $contents = Get_Web_Contents($url,'GET','','',$header);//百度搜索结果页

        $text = str_replace(array("\r\n", "\r", "\n"), "",$contents['Body']);//合并一行

        $text = preg_replace("/<(style|script)(.*?)<\/(style|script)>/i", "", $text);//去css和js

        preg_match_all('/(on:none;">(.*?)<span class="c-pingjia">|<p><span class="c-showurl">(.*?)百度快照)/',$text,$content);//取搜索记录

        preg_match('/果约(.*?)个/',$text,$nums);


        if(empty($content[0])) {
            //0结果
            preg_match('/class="nors"(.*?)<\/div>/',$contents['Body'],$content);
            if(!empty($content[0])){
                print_f("0结果");
                return;
            }
            //出验证码了
            preg_match('/verifycode(.*?)<\/form>/',$contents['Body'],$content);
            if(!empty($content[0])){
                print_f("百度验证码");
                file_put_contents($filepath,"End");
                exit;
            }
        }
        echo count($content[0])."\n";//每页搜索结果
        //每条搜索记录
        foreach($content[0] as $k => $v){

            //取网站域名
            preg_match("/>([^\/]+\.)?({$whitelist})\//",$v,$url);

            //白名单
            if(!empty($url)){
                echo "whitelist \n";
                continue;
            }

            //取网站快照
            preg_match('/" href="h(.*?)\"/',$v,$url);

            echo $k."\t";

            //没快照
            if(empty($url[1])){
                preg_match('/url":"h(.*?)\"/',$v,$url);
            }
            //快照
            $finalsnapshot = Get_Web_Contents('h'.$url[1],'GET','','',$header);

            //快照为空
            if(empty($finalsnapshot['Body']) || !empty($finalsnapshot['Error'])){
                $nul++;
                print_f($finalsnapshot['Error']."\n");
                if($nul>10){
                    $nul = 0;
                    sleep(600);
                }
                continue;
            }

            $snapshot = str_replace(array("\r\n", "\r", "\n","\t"), "",$finalsnapshot['Body']);

            $snapshot = mb_convert_encoding($snapshot, 'UTF-8',array("ASCII","GB2312","GBK",'BIG5'));//转码
            
            //快排泛站群
            if(preg_match('#(id="jst"|mt\.yiliaotui\.com|新春植树正当时)#', $snapshot)){
                echo "whitelist \n";
                continue;
            }

            if('h'.$url[1]!= $finalsnapshot['Info']['url']){

                //取302 title url
                preg_match('/<title(.*?)>(.*?)<\/title>/i',$snapshot,$data);

                $data[5] = $data[2];
                $data[2] = $finalsnapshot['Info']['url'];
            }else{

                //取百度快照title url
                preg_match('/id="bd_snap_note">(.*?)>(.*?)<\/a>(.*?)<title(.*?)>(.*?)<\/title>/i',$snapshot,$data);
            }
            
            //去掉百度头
            $snapshot = preg_replace('#.*?id="bd_snap_ln"#', '', $snapshot);

            $snapshot = codeWords($snapshot);//命中词加样式

            if(!empty($snapshot)){

                print_f($data[5]."\n");
                $insert['tasks_id']  = $tasks['id'];
                $insert['tasks_name']= $tasks['name'];
                $insert['search']    = $tasks['search'];
                $insert['keyword']   = $snapshot['count'];
                $insert['title']     = $data[5];
                $insert['url']       = $data[2];
                $insert['pn']        = $pn+1;
                $insert['po']        = $k+1;
                $insert['status']    = 1;
                $insert['addtime']   = time();
                $insert['snapshot']  = $snapshot['snapshot'];

                //提交
                Get_Web_Contents($posturl,'POST',$insert);

            }else{
                print_f("未命中\n") ;
            }
        }

        echo "\n";

        //页数超出最大结果值
        if((($pn+1)*10)>str_replace(',', '',$nums[1]))
            return ;
    }
}
?>
